package com.controller;


import com.entity.Users;
import com.service.CartService;
import com.service.UserService;
import com.util.SafeUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Objects;

/*
* 用户相关接口
* */
@Controller
@RequestMapping("/index")
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private CartService cartService;

    //private GoodService goodService;


    /*
    * 用户注册
    * */
    @GetMapping("/register")
    public String reg(){
        return "/index/register.jsp";
    }
    /*
     * 用户注册
     * */
    @PostMapping("/register")
    public String register(Users user, HttpServletRequest request){
        if(user.getUsername().isEmpty()){
            request.setAttribute("msg","用户名不能为空");
        }else if (Objects.nonNull(userService.getByUsername(user.getUsername()))){
            request.setAttribute("msg","用户名已存在");
        }else{
            userService.add(user);
            request.setAttribute("msg","注册成功 可以去登录");
            return "/index/login.jsp";
        }

        return "/index/register.jsp";
    }

    /*
    * 用户登录
    * */
    @GetMapping("/login")
    public String log(){
        return "/index/login.jsp";
    }

    /*
     * 用户登录
     * */
    @PostMapping("/login")
    public String login(Users user, HttpServletRequest request, HttpSession session){
        Users loginUser=userService.getByUsernameAndPassword(user.getUsername(), user.getPassword());
        if (Objects.isNull(loginUser)){
            request.setAttribute("msg","用户名或密码错误");
            return "/index/login.jsp";
        }
        session.setAttribute("user",loginUser);
        //session.setAttribute("cartCount",ca);
        String referer=request.getHeader("referer");
        System.out.println(referer);
        //return "redirect:index";
        return "/index/index.jsp";
    }

    /*
    * 修改密码
    * */
    @GetMapping("/password")
    public String password(){
        return "/index/password.jsp";
    }

    @PostMapping("/passwordUpdate")
    public String passwordUpdate(String password,String passwordNew,HttpServletRequest request,HttpSession session){
        Users user= (Users) session.getAttribute("user");
        user=userService.get(user.getId());
        if (!user.getPassword().equals(SafeUtil.encode(password))){
            request.setAttribute("msg","原密码错误！");
        }else{
            userService.updatePassword(user.getId(),passwordNew);
            request.setAttribute("msg","密码修改成功!");

        }
        return "/index/password.jsp";
    }

    /*
    * 注销登录
    * */
    @GetMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute("user");
        session.removeAttribute("cartCount");
        return "/index/login.jsp";
    }

    /*
    * 收货地址
    * */
    @GetMapping("/address")
    public String address(){
        return "/index/address.jsp";
    }

    @PostMapping("/addressUpdate")
    public String addressUpdate(String name,String phone,String address,HttpServletRequest request,HttpSession session){
        Users user= (Users) session.getAttribute("user");
        userService.update(user.getId(),name,phone,address);
        session.setAttribute("user",userService.get(user.getId()));
        request.setAttribute("msg","信息修改成功");
        return "/index/address.jsp";
    }

    /*
    * 购物车总金额
    * */
    /*@GetMapping("/cartTotal")
    public @ResponseBody int careTotal(HttpSession session){
        Users user= (Users) session.getAttribute("user");
        return cartService.getTotal(user.getId());
    }*/

    /*
    * 加入购物车
    * */
    @PostMapping("/cartBuy")
    public @ResponseBody boolean cartBuy(int goodId,HttpSession session){
        Users user= (Users) session.getAttribute("user");
        return cartService.save(goodId,user.getId());
    }
    @GetMapping("/cart")
    public String cart(){
        return "/index/cart.jsp";
    }

    /*@PostMapping("/cartAdd")
    public @ResponseBody boolean cartAdd(int id){
        return cartService.add(id);
    }

    @PostMapping("/cartLess")
    public @ResponseBody boolean cartLess(int id){
        return cartService.less(id);
    }*/
}
